%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ANY QUESTIONS CONTACT ME AT zeky@brown.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
%Load the data
p=path
DATA = dataset('File', "ChoiKarivMullerSilverman_randomChoices_PreparedForAfriat.csv", 'Delimiter', ',', 'Headerlines', 0);
format short
%Turn the Data Set into a Matrix
DATA_mtrx=double(DATA);
%Create the vector of outputs
output=zeros(size(DATA,1),1);

%Estimate emax
for i=1:size(DATA,1)
    %Loop that generates the matrix with P and Q. The data comes in format
    %Px1, Py1,Px2,Py2,...,Px25,Py25
    %This loop transforms the data into [Px1,Py1
    %                                    Px2,Py2
    %                                       ...
    %                                    Px25,Py25]
    %And the same for Q
    for k=1:25
        if k==1
            P=DATA_mtrx(i,2*k:2*k+1);
            Q=DATA_mtrx(i,2*k+50:2*k+51);
        else
            P=[P;DATA_mtrx(i,2*k:2*k+1)];
            Q=[Q;DATA_mtrx(i,2*k+50:2*k+51)] ;
        end
    end
    %This part of the loop, for guy i, estimates emax
    output(i,1)=emax(P,Q);
end

RESULT=[DATA_mtrx(:,1),output]
RESULT_DS = mat2dataset(RESULT,'VarNames',{'id_member','emax'});
export(RESULT_DS,'file','random_emax.csv','Delimiter',',')
